var app = getApp();
var util = require('../../utils/util.js');
var glo = require('../../utils/global.js');
var bibleData = require('../../utils/bible.js')
var oldbook = "";
var newbook = "";

// pages/message/message.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dakaObj: [],
    currentTab: 1,
    userInfo: []
  },
  goVerse: function(e) {
    var bookId = e.target.dataset.bookid;
    var chapterId = e.target.dataset.chapterid;
    var bookName = e.target.dataset.bookname;

    var verseid = e.target.dataset.verseid;
    var content = "";
    var keyid = "CH" + "-" + bookId + "-" + chapterId;

    wx.getStorage({
      key: "bk" + bookId,
      success: function(res) {
        var chapterCount = res.data.length;
        wx.showLoading({
          title: "正在加载...",
          mask: true
        });
        var bookData = res.data;
        for (var i = 0; i < bookData.length; i++) {
          if (chapterId == bookData[i].chapterId) {
            content = bookData[i].content;
            console.log("缓存读取" + bookName + "第" + chapterId + "章");
            wx.navigateTo({
              url: '../content/content' + '?bookName=' + bookName + '&chapterId=' + chapterId + '&chapterCount=' + chapterCount + '&bookId=' + bookId + '&content=' + content + '&lid=' + verseid + '&from=3',
              success: function(res) {
                wx.hideLoading();
                // success
              },
              fail: function() {
                wx.hideLoading();
                // fail
              }
            })
          }
        }
      },
      fail: function() {
        wx.showLoading({
          title: "首次加载该卷",
          mask: true
        });
        wx.request({
          url: glo.https + '/chapter/list_bookid?bookId=' + bookId,
          method: 'GET',
          header: {
            'Content-Type': 'application/json'
          },
          success: function(res) {
            wx.setStorage({ //添加缓存
              key: "bk" + bookId,
              data: res.data
            });
            var bookData = res.data;
            for (var i = 0; i < bookData.length; i++) {
              if (chapterId == bookData[i].chapterId) {
                content = bookData[i].content;
                console.log("缓存读取" + bookName + "第" + chapterId + "章");
                wx.navigateTo({
                  url: '../content/content' + '?bookName=' + bookName + '&chapterId=' + chapterId + '&chapterCount=' + chapterCount + '&bookId=' + bookId + '&content=' + content + '&lid=' + verseid + '&from=3',
                  success: function(res) {
                    wx.hideLoading();
                    // success
                  },
                  fail: function() {
                    wx.hideLoading();
                    wx.showToast({
                      title: '加载失败，请检查网络',
                      icon: 'loading',
                      duration: 1000
                    })
                    // fail
                  }
                })
              }
            }
          }
        })
      }
    });
  },

  clearEdit:function(){
    const that=this;
    wx.showModal({
      title: '您确定清空浏览记录吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          for (let  i = 0; i < 25; i++) {
            wx.setStorageSync("lsr" + i, '0');
          }
          that.setData({
            dakaObj: [],
          });
        } 
      }
    })
    
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bibleRead = wx.getStorageSync('bibleRead');

    oldbook = bibleData.oldbooks;
    newbook = bibleData.newbooks;

    var that = this;
    app.getUserInfo(function(userInfo) {
      that.data.userInfo = userInfo;
    });
    var punch = that.data.userInfo.punch;

    var gdd = glo.getTopNRead(5);
    
    var ppp=[];
    var ii=0;
    for(var i=0;i<25;i++){
      var v=glo.getLastRead(i);
      if(v.length>6){
        ppp[ii]=v;
        ii=ii+1
        if(ii===20)
        {
          break;
        }
      }
    }
    var punchArr01 =ppp;//[];// punch.replace(/#/g, "").split(';').slice(1);
    
    var punchArr_0102 = []
   punchArr01.forEach(function(item, index) {
      var arr000=item.split('-');
      var time1=arr000[0]+'-'+arr000[1]+'-'+arr000[2];
      var text1 = arr000[3]+'-'+arr000[4];
      var bookId1 = arr000[3];
      var chapterId1=arr000[4];
      punchArr_0102.push({
        time: time1,
        text: text1,
        bookId: bookId1,
        chapterId: chapterId1
      });
     /* punchArr_0102.sort(function(a, b) { //时间排序
        var t01 = new Date(a.time);
        var t02 = new Date(b.time)
        return t02 - t01
      })*/
    });

  //  punchArr_0102 = punchArr01;
    var punchArr_02 = [];
    //筛选时间
    for (var i = 0; i < punchArr_0102.length; i++) {
      var time = punchArr_0102[i].time;
      var isHas = false;
      for (var j = 0; j < punchArr_02.length; j++) {
        if (punchArr_02[j].time == time) {
          punchArr_02[j].data.push(punchArr_0102[i]);
        /*  punchArr_02[j].data.sort(function(a, b) {
            return a.bookId - b.bookId
          });*/
          isHas = true;
        }
      }
      if (!isHas) {
        punchArr_02.push({
          time: time,
          selDelbool: false,
          data: [punchArr_0102[i]]
        });
      }
    }
    //筛选章节	 
    
    punchArr_02.forEach(function(item) {
      item.arrData = [];
      var currBookId = 0;
      var currInx = -1;
      item.data.forEach(function(obj, index) {
        var bookId = obj.bookId
        var isHasBID = false;
        if(bookId==currBookId){
          item.arrData[currInx].data.push(obj.chapterId);
          isHasBID=true;
        }
        // item.arrData.forEach(function(objA, inx) {
        //   if (objA.bookId == bookId) {
        //     isHasBID = true;
        //     item.arrData[inx].data.push(obj.chapterId);
        //     /* item.arrData[inx].data.sort(function(a, b) {
        //        return a - b;
        //      })*/
        //   }
        // });
        if (!isHasBID) {
          if (bookId > 39) {
            var bookName = newbook[bookId - 40].bookName
          } else {
            var bookName = oldbook[bookId - 1].bookName
          }
          item.arrData.push({
            tit: bookName,
            bookId: bookId,
            selData: [],
            data: [obj.chapterId]
          });
          currInx=currInx+1;
          currBookId=bookId;
        }
      });
    });
    //console.log(punchArr_02)
    that.setData({
      dakaObj: punchArr_02,
      oData: punchArr_02
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


  /** 
   * 滑动切换tab 
   */
  bindChange: function(e) {
    console.log(e.detail.current)
    console.log(this.data.currentTab)

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },
  /**
   * 用户点击右上角
   */
  onShareAppMessage: function() {
    var v = "菜单";
    if (this.data.pageEnable) {
      v = "%E5%9C%A3%E7%BB%8F"
      v = decodeURI(v)
    }
    return {

      title: ' 这是一款免下载的小程序' + v + ',点开即可马上使用！',

      desc: '微信小程序版本' + v,

      path: '/pages/index/index'

    }


  },
  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  }
})